package cn.doitedu.hbase.hfilemr;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.RegionLocator;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.CellSortReducer;
import org.apache.hadoop.hbase.mapreduce.HFileOutputFormat2;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;

/**
 * @date: 2019/7/12
 * @site: www.doitedu.cn
 * @author: hunter.d 涛哥
 * @qq: 657270652
 * @description:  只负责生成HFIle
 */
public class Json2HFile {

    // ImmutableBytesWritable类需要引入依赖  : hbase-mapreduce
    public static class M extends Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

            // {"id":"6","name":"zs6","age":186,"gender":"F"}
            String s = value.toString();
            ObjectMapper om = new ObjectMapper();
            Person p = om.readValue(s, Person.class);


            // 构造一个rowkey作为map的输出key  ，构造一个keyvalue 作为map的输出value
            ImmutableBytesWritable rowkey = new ImmutableBytesWritable(p.getId().getBytes());
            KeyValue kv1 = new KeyValue(p.getId().getBytes(), "f".getBytes(), "name".getBytes(), p.getName().getBytes());
            KeyValue kv2 = new KeyValue(p.getId().getBytes(), "f".getBytes(), "age".getBytes(), Bytes.toBytes(p.getAge()));
            KeyValue kv3 = new KeyValue(p.getId().getBytes(), "f".getBytes(), "gender".getBytes(), p.getName().getBytes());

            context.write(rowkey,kv1);
            context.write(rowkey,kv2);
            context.write(rowkey,kv3);
        }
    }


    public static void main(String[] args) throws Exception {

        final TableName tableName = TableName.valueOf("person");

        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum","spark01:2181,spark02:2181,spark03:2181");

        Job job = Job.getInstance(conf);
        job.setJarByClass(Json2HFile.class);
        job.setMapperClass(M.class);
        job.setReducerClass(CellSortReducer.class);

        job.setMapOutputKeyClass(ImmutableBytesWritable.class);
        job.setMapOutputValueClass(KeyValue.class);

        FileInputFormat.setInputPaths(job,new Path("hdfs://spark01:8020/json/input"));
        FileOutputFormat.setOutputPath(job,new Path("hdfs://spark01:8020/json/output"));


        Connection conn = ConnectionFactory.createConnection(conf);

        // 用于获取表的region信息
        RegionLocator reginLocator = conn.getRegionLocator(tableName);

        Table table = conn.getTable(tableName);

        // 为job配置表schema信息，region信息
        HFileOutputFormat2.configureIncrementalLoad(job,table,reginLocator);

        boolean res = job.waitForCompletion(true);

        if(!res){
            throw new RuntimeException();
        }






    }



}
